/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android;

import java.util.Locale;

import lt.marsrutai.mobile.android.manager.LanguagePreferenceManager;

import org.acra.ACRA;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;
import org.acra.sender.HttpSender.Method;
import org.acra.sender.HttpSender.Type;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import com.inmobi.adtracker.androidsdk.IMAdTracker;

@ReportsCrashes(
    formKey = "dEZYTi1zZEExa2NjbllGTHZfQmh3aVE6MQ",
    formUri = "https://marsrutai.iriscouch.com/acra-marsrutai/_design/acra-storage/_update/report",
    reportType = Type.FORM,
    httpMethod = Method.PUT,
    formUriBasicAuthLogin="acrareporter",
    formUriBasicAuthPassword="acraREP",
    // Your usual ACRA configuration
    mode = ReportingInteractionMode.SILENT
)
public class MarsrutaiLTApp extends Application {

    private static MarsrutaiLTApp instance;
    
    private static String INMOBI_APP_ID = "82d74d01-6ceb-41df-9eba-22fe9136c5cd";

    public MarsrutaiLTApp() {
        instance = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        AppContext.init(instance);

        ACRA.init(this);

        IMAdTracker.getInstance().init(getApplicationContext(), INMOBI_APP_ID);
        IMAdTracker.getInstance().reportAppDownloadGoal();

        Locale userSetLocale = LanguagePreferenceManager.resolveActiveAppLocale(this);

        Configuration config = getResources().getConfiguration();

        if (!Locale.getDefault().equals(userSetLocale)) {
            config.locale = userSetLocale;

            getResources().updateConfiguration(config, getResources().getDisplayMetrics());
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Locale userSetLocale = LanguagePreferenceManager.resolveActiveAppLocale(this);

        if (!Locale.getDefault().equals(userSetLocale)) {
            Configuration config = new Configuration(newConfig);

            config.locale = userSetLocale;

            getResources().updateConfiguration(config, getResources().getDisplayMetrics());
        }
    }

    public static Context getContext() {
        return instance;
    }

}
